import { defineStore } from 'pinia'
import { remove } from 'lodash-es'

export const useOrderStore = defineStore('order', {
	persist: {
	    enabled: true,
		detached: true, // 设置订阅与组件分离
	},
    state: () => {
		return {
			list: [],
			cosmeticianList: [], // 化妆师列表
			cameramanList: [], // 摄影师列表
			clearDay: null, // 自动清除的天
		}
    },
    actions: {
		setClearDay(num){
			this.clearDay = num
		},
		addOrder(order){
			this.list.unshift(order)
		},
		deleteOrder(id){
			this.list = remove(this.list, (item)=>item.timestamp!==id)
		},
		editOrder(order){
			const index = this.list.findIndex(item=>item.timestamp===order.timestamp)
			if(index>-1){
				this.list[index] = order
			}
		},
		clearYesterdayOrder(){
			this.list = []
		},
		addCosmetician(name, type){
			const li = type===1 ? this.cosmeticianList : this.cameramanList
			if(!li.some(item=>item===name)){
				li.push(name)
			}
		},
		deleteCosmetician(i, type){
			(type===1 ? this.cosmeticianList : this.cameramanList).splice(i, 1);
		},
		changeStatus(id, code){
			const index = this.list.findIndex(item=>item.timestamp===id)
			if(index>-1){
				this.list[index].leaseStatus = code
			}
		}
    }
})